package vn.edu.hungvuongaptech.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import vn.edu.hungvuongaptech.common.Constant;
import vn.edu.hungvuongaptech.dao.UserDAO;

/**
 * Servlet implementation class ChangePasswordController
 */
public class ChangePasswordController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChangePasswordController() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserDAO userDAO=new UserDAO();
		String oldpass=request.getParameter("txtOldPass").toString();
		String newpass=request.getParameter("txtNewPass").toString();
		String confnewpass=request.getParameter("txtConfNewPass").toString();
		HttpSession session=request.getSession(true);
		String susername=session.getAttribute(Constant.USERNAME_ATT).toString();
		if(newpass.equals(confnewpass)){
			int success=userDAO.updatePassword(susername, oldpass, newpass);
			if(success==0){
					session.invalidate(); 
					response.sendRedirect(Constant.PATH_RES.getString("timesheet.changePasswordSuccessPath"));
			}else
				 response.sendRedirect(Constant.PATH_RES.getString("timesheet.changePasswordPath")+"?msg=Old password is wrong!!!");
		}else{
			response.sendRedirect(Constant.PATH_RES.getString("timesheet.changePasswordPath")+"?msg=new password confirmed incorrectly!!!");
		}
			
		
	}

}
